<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">


<head th:include="header">
    <title>定时任务管理</title>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>定时任务管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row row-lg">
                        <div class="col-sm-12">
                            <div class="hidden-xs" id="scheduleDataGridToolbar" ScheduleEntity="group">
                                <button shiro:hasRole="admin" class="btn btn-sm btn-primary" type="submit"
                                        onclick="ScheduleEntity.openAddSchedule()">
                                    <i class="fa fa-pencil"></i>添加
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-info" type="submit"
                                        onclick="ScheduleEntity.openChangeSchedule()">
                                    <i class="fa fa-paste"></i>更新Cron表达式
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit"
                                        onclick="ScheduleEntity.delSchedule()">
                                    <i class="fa fa-cut"></i>删除
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit" onclick="ScheduleEntity.pauseJob()">
                                    <i class="fa fa-pause"></i>暂停
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit"
                                        onclick="ScheduleEntity.resumeJob()">
                                    <i class="fa fa-repeat"></i>恢复
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit"
                                        onclick="ScheduleEntity.startNowJob()">
                                    <i class="fa fa-star-half"></i>立即执行一次
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit" onclick="ScheduleEntity.resetJob()">
                                    <i class="fa fa-repeat"></i>重启所有任务
                                </button>
                                <button shiro:hasRole="admin" class="btn btn-sm btn-danger" type="submit" onclick="ScheduleEntity.stopJob()">
                                    <i class="fa fa-stop"></i>停止所有任务
                                </button>
                            </div>
                            <table id="scheduleDataGrid"/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="footer"></div>
<script type="text/javascript" th:inline="javascript">
    /**
     * 定时任务管理单例对象
     */
    var ScheduleEntity = {
        id: "scheduleDataGrid",    //表格id
        seItem: null,		//选中的条目
        table: null,
        layerIndex: -1
    };

    /**
     * 初始化表格的列
     */
    ScheduleEntity.initColumn = function () {
        var columns = [
            {field: 'selectItem', radio: true},
            {title: '任务组', field: 'jobGroup', align: 'center', valign: 'middle', sortable: true},
            {title: '任务名', field: 'jobName', align: 'center', valign: 'middle', sortable: true},
            {title: 'cron表达式', field: 'cronExpression', align: 'center', valign: 'middle', sortable: true},
            {
                title: '状态',
                field: 'status',
                align: 'center',
                valign: 'middle',
                sortable: true,
                formatter: function (value, row, index) {
                    switch (value) {
                        case "NORMAL":
                            return '正常';
                        case "PAUSED":
                            return '暂停';
                        default:
                            return "异常";
                    }
                }
            },
            {title: '任务执行类名称', field: 'className', align: 'center', valign: 'middle', sortable: true},
            {title: '任务执行类方法', field: 'methodName', align: 'center', valign: 'middle', sortable: true},
            {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true}];
        return columns;
    };

    /**
     * 检查是否选中
     */
    ScheduleEntity.check = function () {
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        if (selected.length == 0) {
            hdw.info("请先选中表格中的某一记录！");
            return false;
        } else {
            ScheduleEntity.seItem = selected[0];
            return true;
        }
    };

    /**
     * 点击添加定时任务
     */
    ScheduleEntity.openAddSchedule = function () {
        var index = layer.open({
            type: 2,
            title: '添加定时任务',
            area: ['35%', '80%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: basePath + 'schedule/addPage'
        });
        this.layerIndex = index;
    };

    /**
     * 点击修改按钮时
     * @param id id
     */
    ScheduleEntity.openChangeSchedule = function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '编辑定时任务',
                area: ['25%', '35%'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: basePath + 'schedule/editPage/' + ScheduleEntity.seItem.jobName + "/" + ScheduleEntity.seItem.jobGroup
            });
            this.layerIndex = index;
        }
    };

    /**
     * 删除任务
     */
    ScheduleEntity.delSchedule = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/delete", function (data) {
                    if (data.success) {
                        hdw.success("删除成功!");
                    } else {
                        hdw.error("删除失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("jobName", ScheduleEntity.seItem.jobName);
                ajax.set("jobGroup", ScheduleEntity.seItem.jobGroup);
                ajax.start();
            };
            hdw.confirm("是否删除任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };

    /**
     * 暂停任务
     */
    ScheduleEntity.pauseJob = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/stop", function (data) {
                    if (data.success) {
                        hdw.success("暂停成功!");
                    } else {
                        hdw.error("暂停失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("暂停失败!" + data.responseJSON.message + "!");
                });
                ajax.set("jobName", ScheduleEntity.seItem.jobName);
                ajax.set("jobGroup", ScheduleEntity.seItem.jobGroup);
                ajax.start();
            };
            hdw.confirm("是否暂停任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };

    /**
     * 恢复任务
     */
    ScheduleEntity.resumeJob = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/resume", function (data) {
                    if (data.success) {
                        hdw.success("恢复成功!");
                    } else {
                        hdw.error("恢复失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("恢复失败!" + data.responseJSON.message + "!");
                });
                ajax.set("jobName", ScheduleEntity.seItem.jobName);
                ajax.set("jobGroup", ScheduleEntity.seItem.jobGroup);
                ajax.start();
            };
            hdw.confirm("是否恢复任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };
    /**
     * 立即执行一次任务
     */
    ScheduleEntity.startNowJob = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/startNow", function (data) {
                    if (data.success) {
                        hdw.success("执行成功!");
                    } else {
                        hdw.error("执行失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("执行失败!" + data.responseJSON.message + "!");
                });
                ajax.set("jobName", ScheduleEntity.seItem.jobName);
                ajax.set("jobGroup", ScheduleEntity.seItem.jobGroup);
                ajax.start();
            };
            hdw.confirm("是否立即执行一次任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };

    /**
     * 重启所有任务
     */
    ScheduleEntity.resetJob = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/restart", function (data) {
                    if (data.success) {
                        hdw.success("重启所有任务成功!");
                    } else {
                        hdw.error("重启所有任务失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("重启所有任务失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            hdw.confirm("是否重启所有任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };

    /**
     * 停止所有任务
     */
    ScheduleEntity.stopJob = function () {
        if (this.check()) {
            var operation = function () {
                var ajax = new $ax(basePath + "schedule/stopAll", function (data) {
                    if (data.success) {
                        hdw.success("停止所有任务成功!");
                    } else {
                        hdw.error("停止所有任务失败!" + data.msg + "!");
                    }
                    ScheduleEntity.table.refresh();
                }, function (data) {
                    hdw.error("停止所有任务失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            hdw.confirm("是否停止所有任务" + ScheduleEntity.seItem.jobName + "?", operation);
        }
    };

    $(function () {
        var defaultColunms = ScheduleEntity.initColumn();
        var table = new BSTable(ScheduleEntity.id, basePath + "schedule/dataGrid", defaultColunms);
        table.setPaginationType("server");
        table.init();
        ScheduleEntity.table = table;
    });
</script>
</body>

</html>